package com.jianyun.jyzs.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.jianyun.jyzs.R;

import java.util.List;

/**
 * Created by mlli on 2017/8/29.
 */

public class LeftAdapter extends BaseAdapter {

    private Context act;
    private List<String[]> data;
    private LeftTagChangeListener leftTagChangeListener;

    public LeftAdapter(Context act,List<String[]> data) {
        super();
        this.act = act;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }
    public interface LeftTagChangeListener{
        void changeIndex(int index);
    }

    public void setLeftTagChangeListener(LeftTagChangeListener leftTagChangeListener) {
        this.leftTagChangeListener = leftTagChangeListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        UserViewHolder mHolder = null;
        if(convertView == null) {
            convertView = View.inflate(act, R.layout.item_crm_search_part_left, null);
            mHolder = new UserViewHolder();
            mHolder.head = (ImageView) convertView.findViewById(R.id.head);
            mHolder.headName = (TextView) convertView.findViewById(R.id.headName);
            convertView.setTag(mHolder);
        } else {
            // 取出holder类
            mHolder = (UserViewHolder) convertView.getTag();
        }
        final String[] ary = data.get(position);
        // 给mHolder类中的对象赋值.
        if(ary[0].equals("1")){
            mHolder.head.setVisibility(View.VISIBLE);
        }else{
            mHolder.head.setVisibility(View.GONE);
        }
        mHolder.headName.setText(ary[1]);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearOther();
                ary[0]="1";
                notifyDataSetChanged();
                if (leftTagChangeListener!=null)leftTagChangeListener.changeIndex(position);
            }
        });
        return convertView;
    }

    private void clearOther() {
        for (String[] st:data
             ) {
            st[0]="0";
        }
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    class UserViewHolder {
        public TextView headName;
        public ImageView head;
    }
}
